<?php
$CI = &get_instance();
?>
<form method="post">
	<input type="radio" name="thongke" value="theongay" <?php if($CI->input->post('thongke') == 'theongay') echo 'checked'; ?>/>Theo ngày
    <input type="radio" name="thongke" value="theothang" <?php if($CI->input->post('thongke') == 'theothang') echo 'checked'; ?>/>Theo tháng
    <br>
<?php
	echo form_date(array('name'=>'ngay'));
?>
    <input type="submit" name="dongy" value="Đồng ý"/>
</form>

<?php
	if($CI->input->post('dongy'))
	{
		$time = get_form_date_value('ngay');
		$ngay = date('d', $time);
		$thang = date('m', $time);
		$nam = date('Y', $time);
		if($CI->input->post('thongke') == 'theongay')
		{
			$query = $CI->db->query("select count(id) soluong from tbl_sim where year(ngayban)=$nam and month(ngayban)=$thang and day(ngayban)=$ngay and daban=1");
			$soluong = $query->result();
			$soluong = $soluong[0]->soluong;
			echo 'Số sim bán ra trong ngày: ' . $soluong . '<br>';
			
			$query = $CI->db->query("select sum(gia) tongtien from tbl_sim where year(ngayban)=$nam and month(ngayban)=$thang and day(ngayban)=$ngay and daban=1");
			$tongso = $query->result();
			$tongso = $tongso[0]->tongtien;
			echo 'Tổng doanh thu trong ngày: ' . $tongso . '<br>';
			
			$query = $CI->db->query("select * from tbladmin where active=1");
			$danhsachtaikhoan = $query->result();
			foreach($danhsachtaikhoan as $taikhoan)
			{
				echo $taikhoan->Tenadmin . ':<br>';
				$query = $CI->db->query("select count(id) soluong from tbl_sim where year(ngayban)=$nam and month(ngayban)=$thang and day(ngayban)=$ngay and daban=1 and nguoiban=" . $taikhoan->ID_admin);
				$soluong = $query->result();
				$soluong = $soluong[0]->soluong;
				echo '&nbsp;&nbsp;&nbsp;Số sim bán ra trong ngày: ' . $soluong . '<br>';
				
				$query = $CI->db->query("select sum(gia) tongtien from tbl_sim where year(ngayban)=$nam and month(ngayban)=$thang and day(ngayban)=$ngay and daban=1 and nguoiban=" . $taikhoan->ID_admin);
				$tongso = $query->result();
				$tongso = $tongso[0]->tongtien;
				echo '&nbsp;&nbsp;&nbsp;Tổng doanh thu trong ngày: ' . $tongso . '<br>';
			}
		}
		else
		{
			$query = $CI->db->query("select count(id) soluong from tbl_sim where year(ngayban)=$nam and month(ngayban)=$thang and daban=1");
			$soluong = $query->result();
			$soluong = $soluong[0]->soluong;
			echo 'Số sim bán ra trong tháng: ' . $soluong . '<br>';
			
			$query = $CI->db->query("select sum(gia) tongtien from tbl_sim where year(ngayban)=$nam and month(ngayban)=$thang and daban=1");
			$tongso = $query->result();
			$tongso = $tongso[0]->tongtien;
			echo 'Tổng doanh thu trong tháng: ' . $tongso . '<br>';
			
			$query = $CI->db->query("select * from tbladmin where active=1");
			$danhsachtaikhoan = $query->result();
			foreach($danhsachtaikhoan as $taikhoan)
			{
				echo $taikhoan->Tenadmin . ':<br>';
				$query = $CI->db->query("select count(id) soluong from tbl_sim where year(ngayban)=$nam and month(ngayban)=$thang and daban=1 and nguoiban=" . $taikhoan->ID_admin);
				$soluong = $query->result();
				$soluong = $soluong[0]->soluong;
				echo '&nbsp;&nbsp;&nbsp;Số sim bán ra trong tháng: ' . $soluong . '<br>';
				
				$query = $CI->db->query("select sum(gia) tongtien from tbl_sim where year(ngayban)=$nam and month(ngayban)=$thang and daban=1 and nguoiban=" . $taikhoan->ID_admin);
				$tongso = $query->result();
				$tongso = $tongso[0]->tongtien;
				echo '&nbsp;&nbsp;&nbsp;Tổng doanh thu trong tháng: ' . $tongso . '<br>';
			}
		}
	}
?>